package com.aiju.widgit;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiju.base.R;
import com.aiju.utils.L;
import com.aiju.utils.Util;

/**
 * @author: 佐为
 * @date: 2016-12-12 11:57
 * @version: V1.0
 * @Description: 购物车加减器
 */
public class ShoppingView extends LinearLayout implements View.OnClickListener {

    public static final int ALL = 0;
    public static final int ADD = 1;
    public static final int SUBTRACT = 2;

    private int sum = 0;
    private int showType;
    private TextView tvAmount;
    private TextView weighTextView;
    private View btnLeft;
    private View btnRight;

    public ShoppingView(Context context) {
        super(context);
        init(context);
    }

    public ShoppingView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ShoppingView);
        showType = ta.getInt(R.styleable.ShoppingView_type, ADD);
        ta.recycle();

        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_amout, this);
        weighTextView = (TextView) findViewById(R.id.tv_shop_weigh);
        weighTextView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        tvAmount = (TextView) findViewById(R.id.tv_shop_num);

        btnLeft = findViewById(R.id.iv__shop_left);
        btnRight = findViewById(R.id.iv__shop_right);
        btnLeft.setOnClickListener(this);
        btnRight.setOnClickListener(this);

        if (showType == ADD) {
            tvAmount.setVisibility(GONE);
            btnLeft.setVisibility(GONE);
        }

    }

    /**
     * 设置称重
     *
     * @param weigh
     */
    public void setShowWeigh(Double weigh) {
        this.showType = SUBTRACT;
        if (weigh == 0) {
            weighTextView.setVisibility(GONE);
            tvAmount.setVisibility(GONE);
            btnRight.setVisibility(VISIBLE);
            btnLeft.setVisibility(GONE);
            return;
        }
        this.sum = 1;
        weighTextView.setVisibility(VISIBLE);
        weighTextView.setText(Util.clearZero(weigh));
        tvAmount.setVisibility(GONE);
        btnRight.setVisibility(GONE);
        btnLeft.setVisibility(VISIBLE);
    }

    public void setSum(int data) {
        weighTextView.setVisibility(GONE);
        if (data==0){
            tvAmount.setVisibility(GONE);
            btnLeft.setVisibility(GONE);
            btnRight.setVisibility(VISIBLE);
        }else {
            tvAmount.setVisibility(VISIBLE);
            tvAmount.setText(String.valueOf(data));
            btnLeft.setVisibility(VISIBLE);
            btnRight.setVisibility(VISIBLE);
        }

        this.sum = data;
        if (showType == ADD) {
            if (sum != 0) {
                tvAmount.setVisibility(VISIBLE);
                btnLeft.setVisibility(VISIBLE);
            } else {
                tvAmount.setVisibility(GONE);
                btnLeft.setVisibility(GONE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv__shop_left) {
            if (sum <= 0) {
                return;
            }
            sum -= 1;
            tvAmount.setText(String.valueOf(sum));

            //默认加号
            if (showType == ADD) {
                if (sum == 0) {
                    btnLeft.setVisibility(GONE);
                    tvAmount.setVisibility(GONE);
                }
            } else if (showType == SUBTRACT) {
                tvAmount.setVisibility(GONE);
                btnLeft.setVisibility(GONE);
                btnRight.setVisibility(VISIBLE);
                weighTextView.setVisibility(VISIBLE);
                weighTextView.setText("");
            }

            //监听
            if (listener != null) {
                listener.onAmountChange(SUBTRACT, sum);
            }
        } else if (v.getId() == R.id.iv__shop_right) {
            L.d("加");
            sum += 1;
            tvAmount.setText(String.valueOf(sum));

            //默认加号
            if (showType == ADD) {
                if (sum > 0 && btnLeft.getVisibility() == GONE) {
                    btnLeft.setVisibility(VISIBLE);
                    tvAmount.setVisibility(VISIBLE);
                }
            }
            //监听
            if (listener != null) {
                listener.onAmountChange(ADD, sum);
            }
        }
    }

    public TextView getWeighTextView() {
        return weighTextView;
    }

    private OnSumChange mOnSumChange;

    public void setOnSumChange(OnSumChange onSumChange) {
        this.mOnSumChange = onSumChange;
    }

    public interface OnSumChange {
        void onSumChange(int sum);
    }

    //====加减器
    private OnAmountChangeListener listener;

    public void setOnAmountChangeListener(OnAmountChangeListener listener) {
        this.listener = listener;
    }

    public interface OnAmountChangeListener {
        void onAmountChange(int type, int sum);
    }

}
